<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once "config.php";

class Database extends Config {
	private $db_link;
	private $db_sel;
	
	private $db_erg = null;
	private $index = 0;
	private $rows = 0;
	private $data;

	function __construct(){
    	$this->db_link = mysql_connect($this->mysql_server ,$this->mysql_user,$this->mysql_password)
             	or die("error: " + mysql_error());
			 //or die(json_encode(array("status" => "error","error" => "mysql","msg" => mysql_error())));
		$this->db_sel = mysql_select_db($this->mysql_db)
             or die("error: " + mysql_error());
			 //or die(json_encode(array("status" => "error","error" => "mysql","msg" => mysql_error())));
	}
	
	public function query($sql){
		//if($this->db_erg != null) mysql_free_result($this->db_erg);
		$this->index = -1;

		$this->db_erg = mysql_query($sql)
							or die("error: " + mysql_error());
		$this->rows = mysql_num_rows($this->db_erg);
	}
	
	private function parseWhere($where){
		if(gettype($where) == 'string'){
			return $where;
		}else if(gettype($where) == 'array'){
			$first = 1;
			$ret = '';
			foreach($where as $key => $value){
	        	if($first == 1){
					$ret .= '`' . $key .'` = \'' . $value . '\'';
					$first = 0;
				}else{
					$ret .= '`' . $key .'` = \'' . $value . '\'';
				}
			}
			echo $ret;
			return $ret;
		}
	}

	public function insert($table,$data){
		$sql = 'INSERT INTO `' . $table .'` (';

		$first = 1;
		foreach($data as $key => $value){
        	if($first == 1){
				$sql .= '`' . $key . '`';
				$first = 0;
			}else{
				$sql .= ',`' . $key . '`';
			}
		}
		
		$sql .= ') VALUES (';

		$first = 1;
		foreach($data as $key => $value){
        	if($first == 1){
				$sql .= '\'' . $value . '\'';
				$first = 0;
			}else{
				$sql .= ',\'' . $value . '\'';
			}
		}

		$sql .= ');';
        //$this->query($sql);
        $this->index = -1;
        $this->rows = 0;
        mysql_query($sql)
        			or die("mysql error: " . mysql_error());
        return mysql_insert_id();
	}
	
	public function update($table,$where,$data){
		$sql = 'UPDATE `' . $table .'` SET ';
		
		$first = 1;
		foreach($data as $key => $value){
        	if($first == 1){
				$sql .= '`' . $key . '` = \'' .$value. '\'';
				$first = 0;
			}else{
				$sql .= ',`' . $key . '` = \'' .$value. '\'';
			}
		}
        $where = $this->parseWhere($where);
		$sql .= ' WHERE ' .$where . ';';
        //$this->query($sql);
        $this->index = -1;
        $this->rows = 0;
        mysql_query($sql)
        			or die("mysql error: " . mysql_error());
        //return mysql_insert_id($this->db_erg);
	}

	public function select($table,$selection = '*',$where = '',$order = '',$limit = ''){
		$sql = 'SELECT ';
		
		if(gettype($selection) == "array"){
			$first = 1;
			foreach($selection as $select){
				if($first == 1){
					$sql .= '`' . $select . '`';
					$first = 0;
				} else {
					$sql .= ',`' . $select . '`';
				}
			}
		}else $sql .= $selection;

		$sql .= ' FROM `'.$table.'`';
		
		$where = $this->parseWhere($where);
		if($where != ''){
        	$sql .= ' WHERE ' . $where;
		}

        if($order != ''){
        	$sql .= ' ORDER BY ' . $order;
		}

        if($limit != ''){
        	$sql .= ' LIMIT ' . $limit;
		}

		$sql .= ';';

		$this->query($sql);
	}
	
	public function delete($table,$where,$limit = ''){
		$sql = 'DELETE FROM `'.$table.'`';
		$where = $this->parseWhere($where);
		$sql .= ' WHERE ' . $where;
		if($limit != ''){
			$sql .= ' LIMIT ' . $LIMIT;
		}
		$sql .= ';';
        $this->index = -1;
        $this->rows = 0;
        mysql_query($sql)
        			or die("mysql error: " . mysql_error());
	}

	public function rows(){
		return $this->rows;
	}

    public function moveToFirst(){
		$this->index = 0;
		mysql_data_seek($this->db_erg,$this->index);
		$this->data = mysql_fetch_array($this->db_erg);
	}

    public function moveToLast(){
		$this->index = $this->rows-1;
		mysql_data_seek($this->db_erg,$this->index);
		$this->data = mysql_fetch_array($this->db_erg);
	}

	public function moveToNext(){
		if($this->index < $this->rows-1){
			$this->index++;
			mysql_data_seek($this->db_erg,$this->index);
			$this->data = mysql_fetch_array($this->db_erg);
			return true;
		} else return false;
	}
	
	public function hasColumn($column){
		if(isset($this->data[$column])){
			return true;
		} else return false;
	}

	public function get($column){
		if($this->hasColumn($column)){
			return $this->data[$column];
		}else return null;
	}
}

?>